package com.cjm.algorithm.tree;

import com.cjm.algorithm.TreeNode;

import java.util.LinkedList;

public class Ti222 {
    public static void main(String[] args) {

    }
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        LinkedList<TreeNode> treeNodes = new LinkedList<>();
        treeNodes.addLast(root);

        int nums = 0;
        while (!treeNodes.isEmpty()) {
            int size = treeNodes.size();
            for (int i = 0; i < size; i++) {
                TreeNode treeNode = treeNodes.removeFirst();
                nums++;
                if (treeNode.left != null) treeNodes.addLast(treeNode.left);
                if (treeNode.right != null) treeNodes.addLast(treeNode.right);
            }
        }
        return nums;
    }
}
